package com.hns.can.master.web;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Collections;
import java.util.Comparator;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.master.entity.CommMftAlmEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.BaseCarInfoEntity;
import com.hns.can.master.entity.CommCarCurrentLoInfoEntity;
import com.hns.can.master.service.OperationManageService;
import com.hns.can.master.vo.DriveTrajInfoVo;

/**
 * 
 * @版权：HNS 版权所有 (c) 2013
 * @author:Lan
 * @version Revision 1.0.0
 * @email:763836737@qq.com
 * @see:
 * @创建日期：2015-6-17
 * @功能说明：线路参数设置
 * @begin
 * @修改记录:
 * @修改后版本          修改人      	修改内容
 * @2015-6-17  	        Lan        	创建
 * @end
 */
@Controller
@RequestMapping("/operationManage")
public class OperationManageController extends BaseController {
    
    @Autowired
    private OperationManageService operationManageService;
    @Autowired
    private CommonInitParamService commonInitParamService;
    
    /**
     * 获取车辆对应公司id和线路id
     * @param params
     * @author:zhangy
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getCorpAndLine")
    @ResponseBody
    public Object getCorpAndLine(CommonParamsVo params){
        List<BaseCarInfoEntity> list = operationManageService.getCorpAndLine(params);
	    return this.getReturnResult(list.get(0));
    }
    
    /**
     * 获取行驶记录轨迹
     * @param entity
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
     @RequestMapping("/getDriveTrajInfo2")
     @ResponseBody
     public Object getDriveTrajInfo2(CommonParamsVo entity) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
    	entity.setCarIds(new String[]{entity.getCarId()});
		commonInitParamService.initParamsLimitCar(entity,user,null);
		List<DriveTrajInfoVo> list = operationManageService.getDriveTrajInfo(entity,commonInitParamService);
		return this.getReturnResult(list);
    }

    /**
     * 获取行驶记录轨迹、故障明细、驾驶行为明细
     * @param entity
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getDriveTrajInfo")
    @ResponseBody
    public Object getDriveTrajInfo(CommonParamsVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCarIds(new String[]{vo.getCarId()});
        commonInitParamService.initParamsLimitCar(vo,user,null);
        //行驶记录轨迹
        List<DriveTrajInfoVo> list = operationManageService.getDriveTrajInfo(vo,commonInitParamService);
        //故障明细
        commonInitParamService.initParamsLimitCar(vo,user, Arrays.asList("T_COMM_MFT_ALM"));
        List<DriveTrajInfoVo> list2 = operationManageService.getAllRealTimeFailure2(vo);
        //驾驶行为明细
        List<DriveTrajInfoVo> list3 = operationManageService.getDrivingBehaviorList2(vo);
        if(list2!=null){
            list.addAll(list2);
        }
        if(list3!=null){
            list.addAll(list3);
        }
        if(list!=null) {
            Collections.sort(list, new Comparator<DriveTrajInfoVo>() {
                public int compare(DriveTrajInfoVo arg0, DriveTrajInfoVo arg1) {
                    return arg0.getRcrdTime().compareTo(arg1.getRcrdTime());
                }
            });
        }
        return this.getReturnResult(list);
    }

    /**
     * 故障类型
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getAllRealTimeFailure2")
    @ResponseBody
    public Object getAllRealTimeFailure2(CommonParamsVo vo) throws Exception {
        List<DriveTrajInfoVo>  list = operationManageService.getAllRealTimeFailure2(vo);
        return this.getReturnResult(list);
    }

    /**
     * 异常行为
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getDrivingBehaviorList2")
    @ResponseBody
    public Object getDrivingBehaviorList2(CommonParamsVo vo) throws Exception {
        List<DriveTrajInfoVo>  list = operationManageService.getDrivingBehaviorList2(vo);
        return this.getReturnResult(list);
    }

    /**
     * @param params
     * @author:zhangy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2015年9月7日
     * @功能说明：获取线路实时路况
     */
    @RequestMapping("/getRealTimeTrafficStatus")
    @ResponseBody
    public Object getRealTimeTrafficStatus(CommonParamsVo params){
    	SysUserEntity user = this.getCurrentUser().getUser();
    	commonInitParamService.setOrgIds(params, null, user, null);
    	List<CommCarCurrentLoInfoEntity> list = operationManageService.getRealTimeTrafficStatus(params);
    	return this.getReturnResult(list);
    }
    
    
    /**
     * @param params
     * @author:zhangy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2015年9月7日
     * @功能说明：获取线路偏移经纬度数据
     */
    @RequestMapping("/loadShiftRoute")
    @ResponseBody
    public Object loadShiftRoute(CommonParamsVo entity) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
   		entity.setCarIds(new String[]{entity.getCarId()});
		commonInitParamService.initParamsLimitCar(entity,user,null);
		List<DriveTrajInfoVo> list = operationManageService.loadShiftRoute(entity,commonInitParamService);
		return this.getReturnResult(list);
   }
}
